package com.card.framework.util;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import net.sf.json.JSONArray;
import com.card.framework.mybatis.mapper.ExecuteSQLCallBack;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.manager.domain.JsonTreeNode;

@Service("TreeBulid")
public class TreeBulid extends MyFrameworkSupperMapper {
	private static final String sql = "select c.*,t.resouceid from ( SELECT A.resouceid, A.name, A.url, A.ParentID "
			+ ", case when ifnull(B.c, 0) > 0 then 1 else 0 end as HasChild,A.type FROM resources A "
			+ " LEFT JOIN ( SELECT COUNT(1) c ,ParentID AS PID FROM resources MA GROUP BY ParentID) B "
			+ "ON A.resouceid=B.PID WHERE A.ParentID=ParentID   and ParentID = ? )"
			+ " c left join (select resouceid from roles_resources left join users_roles on   users_roles.roleid = roles_resources.roleid where users_roles.userid = ?) t on c.resouceid = t.resouceid "
			+ "Order By c.resouceid";
	private static final String menuSql = "select c.*,t.resouceid from ( SELECT A.resouceid, A.name, A.url, A.ParentID "
		+ ", case when ifnull(B.c, 0) > 0 then 1 else 0 end as HasChild,A.type FROM resources A "
		+ " LEFT JOIN ( SELECT COUNT(1) c ,ParentID AS PID FROM resources MA GROUP BY ParentID) B "
		+ "ON A.resouceid=B.PID WHERE A.ParentID=ParentID   and ParentID = ? )"
		+ " c left join (select resouceid from roles_resources where roles_resources.roleid = ?) t on c.resouceid = t.resouceid "
		+ "Order By c.resouceid";
	public static void main(String[] args) {
		System.out.println(menuSql);
	}
	
	public String buildAllTree(int user,int type,int id) {
		JsonTreeNode rootnode = null;
		List<JsonTreeNode> node = getChildDivide(id, user,type);
		List<JsonTreeNode> list = new ArrayList<JsonTreeNode>();
		int len = node.size();
		if (len > 0) {
			rootnode = node.get(0);
			if(id==-2) rootnode.setState("open");
			if(rootnode.hasChildren) rootnode.setChildren(treeBuildIt(rootnode.getId(),
					user,type));
			list.add(rootnode);
		} else {
			return "";
		}
		//多节点
		for (int i = 1; i < len; i++) {
			rootnode = node.get(i);
			if(rootnode.hasChildren) rootnode.setChildren(treeBuildIt(rootnode.getId(),
					user,type));
			list.add(rootnode);
		}
		JSONArray json = JSONArray.fromObject(list);
		System.out.println(json.toString());
		return json.toString();
	}
	
	
	private List<JsonTreeNode> treeBuildIt(int parentId, int user,int type) {
		List<JsonTreeNode> list = getChildDivide(parentId, user, type);
		for (JsonTreeNode json : list){
				if(json.hasChildren) json.setChildren(treeBuildIt(json.getId(),user,type));
		}
		return list;
	}
	
	
	public List<JsonTreeNode> getChildDivide(final int parentID,
			final int userid,final int type) {
		final List<JsonTreeNode> list = new ArrayList<JsonTreeNode>();
		try {
			System.out.println(sql+"=="+parentID);
			executeSQL(type==1?sql:menuSql, new ExecuteSQLCallBack() {
				@Override
				public void executeSQLCallBack(PreparedStatement pstmt)
						throws SQLException {
					pstmt.setInt(1, parentID);
					pstmt.setInt(2, userid);
					ResultSet res = pstmt.executeQuery();
					while (res.next()) {
						JsonTreeNode da = new JsonTreeNode();
						da.setId(res.getInt(1));
						da.setText(res.getString(2));
						da.setAttributes(res.getString(3));
						da.setHasChildren(res.getInt(5) > 0);
						System.out.println(res.getInt(5) + "==="
								+ res.getString(2));
						da.setType(res.getString(6));
						if(!da.isHasChildren()) da.setState("open");
						int checked = res.getInt(7);
						//type=1 为menu
						if ((!da.hasChildren||type==1)&&checked==da.getId()) {
							System.out
									.println(da.getText()+"-"+da.getAttributes());
							da.setChecked(true);
						} else {
							da.setChecked(false);
						}

						list.add(da);
					}
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
